<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'proj:enex:pmApproach:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <!-- <a-button @click="handlePrint()" v-auth="'proj:enex:pmApproach:edit'">
          <Icon icon="simple-line-icons:printer" /> {{ t('打印开工通知单') }}
        </a-button>

        <a-button type="primary" @click="handlesgForm({status:0})" v-auth="'proj:enex:pmApproach:sendEn'">
          <Icon icon="ant-design:fullscreen-exit-outlined" /> {{ t('发送入场通知') }}
        </a-button> -->
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.pcode }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.pcode }}
        </a>
        
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.createDate }}
        </a>
      </template> -->
      <!-- <template #picUrl="{ record }">
        <a-image :width="50" :src="record.picUrl ? ctxPath + record.picUrl : '/resource/img/noSet.png'" />
      </template> -->

      <!-- v-if="hasPermission(['shop:order:cusFpApply:edit'])" -->
      <template #uploadFile1="{ record }">
        <BasicUpload
          v-if="uploadshow"
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'pmApproachEN_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>

      <template #uploadFile2="{ record }">
        <BasicUpload
          v-if="uploadshow"
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'pmApproachEX_image'"
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :isPreview="false"
          :emptyHidePreview="true"
        />
      </template>
      <template #launchpredate="{ record }">
        {{ formatDate(record.launchpredate) }}
      </template>
      <template #launchprewdate="{ record }">
        {{ formatDate(record.launchprewdate) }}
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <sgform @register="registersgDrawer" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjEnexPmApproachList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, onMounted,watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmApproachDelete, pmApproachListData } from '/@/api/proj/enex/pmApproach';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import sgform from './sgform.vue';

  import PrintModal from '/@/components/print/printModal.vue';
  import { useModal } from '/@/components/Modal';
  import { BasicUpload } from '/@/components/Upload';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.enex.pmApproach');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('进场撤场管理'),
    value: t('进场撤场管理'),
  };
  let uploadshow = ref<any>(true);

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  // 时间格式化日期，只保留年月日
  const formatDate = (date: string) => {
    if (!date) return '';
    const dateObj = new Date(date);
    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1;
    const day = dateObj.getDate();
    return `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day}`;
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编码'),
        field: 'pcode',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('工程名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'state',
        component: 'Select',
        defaultValue: '2',
        componentProps: {
          dictType: 'zf_proj_jcsg_status',
          // allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工程编码'),
      dataIndex: 'pcode',
      key: 'a.pcode',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('工程名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'state',
      key: 'a.state',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'zf_proj_jcsg_status',
      fixed: 'left',
    },

    {
      title: t('通知时间(进)'),
      dataIndex: 'launchpredate',
      key: 'a.launchpredate',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'launchpredate',
    },
    {
      title: t('要求日期(进)'),
      dataIndex: 'predate',
      key: 'a.predate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('确认日期(进)'),
      dataIndex: 'approachdate',
      key: 'a.approachdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('现场图(进)'),
      dataIndex: 'approachedu',
      key: 'a.approachedu',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'uploadFile1',
    },

    {
      title: t('通知时间(撤)'),
      dataIndex: 'launchprewdate',
      key: 'a.launchprewdate',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'launchprewdate',
    },
    {
      title: t('要求日期(撤)'),
      dataIndex: 'prewithdrawdate',
      key: 'a.prewithdrawdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('确认日期(撤)'),
      dataIndex: 'withdrawdate',
      key: 'a.withdrawdate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('现场图(撤)'),
      dataIndex: 'withdrawedu',
      key: 'a.withdrawedu',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'uploadFile2',
    },
    {
      title: t('延期时间'),
      dataIndex: 'delayedu',
      key: 'a.delayedu',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 150,
    actions: (record: Recordable) => [
      // {
      //   icon: 'clarity:note-edit-line',
      //   title: t('确认进场'),
      //   label: t('确认进场'),
      //   color: 'error',
      //   onClick: handlesgForm.bind(this, { id: record.id,pcode:record.pcode,name:record.name, status: 1 }),
      //   auth: 'proj:enex:pmApproach:validEn',
      //   ifShow: () => record.state == '0' || record.state == '1',
      // },
      {
        icon: 'ant-design:fullscreen-outlined',
        title: t('发送撤场通知'),
        label: t('发送撤场通知'),
        color: 'success',
        onClick: handlesgForm.bind(this, {
          id: record.id,
          pcode: record.pcode,
          name: record.name,
          status: 2,
        }),
        auth: 'proj:enex:pmApproach:sendEx',
        ifShow: () => record.state == '2',
        //  || record.state == '3'
      },
      {
        icon: 'clarity:note-edit-line',
        title: t('确认撤场'),
        label: t('确认撤场'),
        color: 'error',
        onClick: handlesgForm.bind(this, {
          id: record.id,
          pcode: record.pcode,
          name: record.name,
          status: 3,
        }),
        auth: 'proj:enex:pmApproach:validEx',
        ifShow: () => record.state == '4',
      },
    ],
  };

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registersgDrawer, { openDrawer: opensgDrawer }] = useDrawer();

  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: pmApproachListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.pcode,
        tabTitle: `项目详情(${record.pcode})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlesgForm(record: Recordable) {
    console.log(record, 'record');
    // uploadshow.value = false
    opensgDrawer(true, record);
  }

  function handleSuccess() {
    reload();
    uploadshow.value = false;
    setTimeout(() => {
      uploadshow.value = true;
    }, 500);
  }

  // 打印
  const handlePrint = () => {
    let rows = getSelectRows();
    if (rows.length <= 0) {
      showMessage('请至少选择一条要打印的数据!');
    } else {
      let ids = '';
      rows.map((item) => {
        if (ids == '') {
          ids = item.id;
        } else {
          ids = ids + ',' + item.id;
        }
      });
      let obj = {
        title: '开工通知单打印',
        ids: ids,
        height: '0.7',
        width: '0.6',
        fileName: 'print-entzd',
      };
      openPrintModal(true, obj);
    }
  };
  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      pcode: props.projectno?props.projectno:userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      pcode: props.projectno?props.projectno:userStore.getProjecte.code,
    });
  }
</script>
